/**
 * 超级管理员首页脚本
 */

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 检查登录状态和权限
    if (!Auth.isLoggedIn()) {
        window.location.href = '../login.html';
        return;
    }

    if (!Auth.checkRole(UserRole.SUPER_ADMIN)) {
        UI.showMessage('您没有权限访问此页面', 'danger');
        setTimeout(() => {
            Auth.logout();
        }, 2000);
        return;
    }

    // 初始化页面
    initPage();
});

/**
 * 初始化页面
 */
async function initPage() {
    // 显示用户信息
    displayUserInfo();
    
    // 显示当前日期
    displayCurrentDate();
    
    // 加载系统概览数据
    await loadSystemOverview();
}

/**
 * 显示用户信息
 */
function displayUserInfo() {
    const userInfo = Auth.getUserInfo();
    if (userInfo) {
        const userName = document.getElementById('userName');
        const welcomeName = document.getElementById('welcomeName');
        const userAvatar = document.getElementById('userAvatar');
        
        if (userName) userName.textContent = userInfo.account || '超级管理员';
        if (welcomeName) welcomeName.textContent = userInfo.account || '超级管理员';
        if (userAvatar) userAvatar.textContent = (userInfo.account || 'S').charAt(0).toUpperCase();
    }
}

/**
 * 显示当前日期
 */
function displayCurrentDate() {
    const currentDate = document.getElementById('currentDate');
    if (currentDate) {
        const now = new Date();
        const options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long' };
        currentDate.textContent = now.toLocaleDateString('zh-CN', options);
    }
}

/**
 * 加载系统概览数据
 */
async function loadSystemOverview() {
    try {
        UI.showLoading();
        
        // 调用系统概览API
        const data = await HttpClient.get('/admin/statistics/overview');
        
        // 更新统计数据
        updateStatistics(data);
        
        UI.hideLoading();
    } catch (error) {
        console.error('加载系统概览数据失败:', error);
        UI.hideLoading();
        UI.showMessage('加载数据失败: ' + error.message, 'danger');
    }
}

/**
 * 更新统计数据
 */
function updateStatistics(data) {
    // 更新总体统计
    if (data.totalUsers !== undefined) {
        document.getElementById('totalUsers').textContent = data.totalUsers;
    }
    if (data.totalAppointments !== undefined) {
        document.getElementById('totalAppointments').textContent = data.totalAppointments;
    }
    if (data.totalSites !== undefined) {
        document.getElementById('totalSites').textContent = data.totalSites;
    }
    if (data.totalVaccines !== undefined) {
        document.getElementById('totalVaccines').textContent = data.totalVaccines;
    }
    
    // 更新预约状态统计
    if (data.pendingAppointments !== undefined) {
        document.getElementById('pendingAppointments').textContent = data.pendingAppointments;
    }
    if (data.completedAppointments !== undefined) {
        document.getElementById('completedAppointments').textContent = data.completedAppointments;
    }
    if (data.cancelledAppointments !== undefined) {
        document.getElementById('cancelledAppointments').textContent = data.cancelledAppointments;
    }
}

/**
 * 退出登录
 */
function handleLogout() {
    UI.confirm('确定要退出登录吗？', () => {
        Auth.logout();
    });
}

